home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / GSRC208A.ZIP / GETALL.C < prev    next >
C/C++ Source or Header  |  1993-08-25  |  27KB  |  880 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992, 1993  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*             data input            */
  11. /*                                   */
  12. /*        Zortech C/C++ 3.0 r4       */
  13. /*          MICROSOFT C 6.00         */
  14. /*          Visual C/C++ 1.0         */
  15. /*           QuickC/WIN 1.0          */
  16. /*             ULTRIX cc             */
  17. /*              GNU gcc              */
  18. /*                                   */
  19. /*   (include here compilers that    */
  20. /*   compiled GEPASI successfully)   */
  21. /*                                   */
  22. /*************************************/
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #ifdef MSDOS
  29. #include <io.h>
  30. #endif
  31. #include <fcntl.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <math.h>
  35. #include "globals.h"
  36. #include "globvar.h"
  37. #include "datab.h"
  38. #include "newton.h"
  39. #include "strtbl.h"
  40. #include "gauss.h"
  41. #include "kinetics.h"
  42. #include "pmu.h"                                    /* several utilities     */
  43. #include "heapchk.h"
  44.  
  45. #if ( _MSC_VER >= 610 )
  46.  #define open _open
  47.  #define close _close
  48.  #define stat _stat
  49.  #define fstat _fstat
  50.  #define read _read
  51.  #define write _write
  52.  #define O_RDONLY _O_RDONLY
  53. #endif
  54.  
  55. #ifdef _ZTC
  56. #define MEM_DEBUG 1
  57. #include "mem.h"
  58. #else
  59. #define mem_malloc malloc
  60. #define mem_free free
  61. #define mem_realloc realloc
  62. #endif
  63.  
  64. /*
  65.     get the list of files to process
  66. */
  67.  
  68. void getlist( char *inpf )
  69. {
  70.  int i;
  71.  FILE *chg;
  72.  
  73.  if ( inpf )
  74.  {
  75.   if (!(chg=fopen(inpf,"r")))
  76.   {
  77.    printf(errormsg[6],inpf);
  78.    term_gepasi();
  79.   }
  80.  }
  81.  else
  82.  {
  83.   chg = stdin;
  84.   fprintf( stderr, "\n\n%s\n\n", gepasi );
  85.   fprintf( stderr, "input filenames one at a line, a dot followed by ENTER ends.\n\n");
  86.  }
  87.  for(i=0;i<MAX_FIL;i++)                              /* stop at MAX_FIL     */
  88.  {
  89.   fgets( filename[i], PWIDTH, chg );
  90.   if (   ( filename[i][0] == '.' )
  91.       || ( feof( chg ) )
  92.      ) break;                                        /* stop if EOF or .    */
  93.   filename[i][strlen(filename[i])-1] = 0;            /* put /0 at the end   */
  94.   fixext( filename[i], '.', ".sim" );
  95.  }
  96.  nrunfiles = i;
  97.  if ( inpf ) fclose( chg );
  98.  return;
  99. }
  100.  
  101.  
  102.  
  103. /*
  104.     read topology from file
  105. */
  106.  
  107. int BufToTop( char *Buff, char **endoftop )
  108. {
  109.  int i,j;
  110.  int daux;
  111.  unsigned char uaux;
  112.  char *paux;
  113.  char saux[NAME_L];
  114.  
  115. /* reset the version number to zero                                                    */
  116.  ver_no = 0;
  117.  /* copy the first line                                                                                 */
  118.  paux = strchr( Buff, '\n' );
  119.  if( paux == NULL ) return IDS_ERR_BAD_TOPNAM;
  120.  /* take care of CR if one existed before LF                                            */
  121.  if( *(paux-1)=='\r' ) *(paux-1) = '\0';
  122.  *paux = '\0';
  123.  if( strlen( Buff ) > 255 ) Buff[255] = '\0';
  124.  strcpy( topname, Buff );
  125.  Buff = paux+1;
  126.  /* is this line a version number ?                                                                     */
  127.  sscanf( topname, "version %f", &ver_no );
  128.  if( ver_no != 0 )
  129.  {
  130.   /* YES! so let's read another line, with the title                            */
  131.   paux = strchr( Buff, '\n' );
  132.   if( paux == NULL ) return IDS_ERR_BAD_TOPNAM;
  133.   if( *(paux-1)=='\r' ) *(paux-1) = '\0';
  134.   *paux = '\0';
  135.   if( strlen( Buff ) > 255 ) Buff[255] = '\0';
  136.   strcpy( topname, Buff );
  137.   Buff = paux+1;
  138.  }
  139.  
  140.  /* read nstep from the buffer                                                                          */
  141.  if ( ( sscanf( Buff, " %d", &daux ) < 1) ||
  142.       ( daux < 1 ) || ( daux > MAX_STEP ) )
  143.   return IDS_ERR_BAD_NSTEP;
  144.  nsteps = daux;
  145.  
  146.  Buff = strchr( Buff, ' ' );
  147.  /* read totmet from the buffer                                                                 */
  148.  if( (Buff == NULL ) || ( sscanf( ++Buff, " %d", &daux ) < 1) ||
  149.      ( daux < 1 )   || ( daux > MAX_MET ) )
  150.   return IDS_ERR_BAD_TOTMET;
  151.  totmet = daux;
  152.  
  153.  /* read stoiu from the buffer                                                                          */
  154.  for(i=0;i<totmet;i++)
  155.   for(j=0;j<nsteps;j++)
  156.   {
  157.    if(j==0)
  158.    {
  159.     Buff = strchr( Buff, '\n' );
  160.     if( Buff != NULL )
  161.     {
  162.      Buff++;
  163.      for( ;*Buff == ' '; Buff++);
  164.     }
  165.    }
  166.    else
  167.    {
  168.     Buff = strchr( Buff, ' ' );
  169.     if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  170.    }
  171.    if ( (Buff == NULL) || (sscanf( Buff, "%d", &daux ) < 1) )
  172.     return IDS_ERR_BAD_STOI;
  173.    stoiu[i][j] = daux;
  174.   }
  175.  Buff = strchr( Buff, '\n' );
  176.  if( Buff == NULL ) return IDS_ERR_BAD_KINTYPE;
  177.  Buff++;
  178.  /* read kinetu, revers and metname from the buffer                             */
  179.  for( i=0; i<nsteps; i++ )
  180.  {
  181.   for( ;*Buff == ' '; Buff++);
  182.   if ( (Buff == NULL) || (sscanf( Buff, "%d", &daux ) < 1)
  183.        || (daux < -1) )
  184.    return IDS_ERR_BAD_KINTYPE;
  185.   kinetu[i] = daux;
  186.   Buff = strchr( Buff, ' ' );
  187.   if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  188.   if ( (Buff == NULL) || (sscanf( Buff, "%d", &daux ) < 1) )
  189.    return IDS_ERR_BAD_KINTYPE;
  190.   revers[i] = daux;
  191.  
  192.   Buff = strchr( Buff, ' ' );
  193.   if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  194.   paux = strchr( Buff, '\n' );
  195.   if( paux == NULL ) return IDS_ERR_BAD_KINTYPE;
  196.   /* take care of CR if one existed before LF                                           */
  197.   if( *(paux-1)=='\r' ) *(paux-1) = '\0';
  198.   *paux = '\0';
  199.   if( strlen( Buff ) >= NAME_L ) Buff[NAME_L-1] = '\0';
  200.   strcpy( stepname[i], Buff );
  201.   Buff = paux+1;
  202.  }
  203.  
  204.  *(--Buff) = '\n';
  205.  /* read rstr from the buffer                                                                           */
  206.  for(i=0;i<nsteps;i++)
  207.   for(j=0;j<MAX_MOL;j++)
  208.   {
  209.    if(j==0)
  210.    {
  211.     Buff = strchr( Buff, '\n' );
  212.     if( Buff != NULL )
  213.     {
  214.      Buff++;
  215.      for( ;*Buff == ' '; Buff++);
  216.     }
  217.    }
  218.    else
  219.    {
  220.     Buff = strchr( Buff, ' ' );
  221.     if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  222.    }
  223.    if ( (Buff == NULL) || (sscanf( Buff,"%d", &daux ) < 1) )
  224.     return IDS_ERR_BAD_LOOP;
  225.    (*rstr)[i][j] = (signed char) daux;
  226.   }
  227.  
  228.  /* read loop from the buffer                                                                           */
  229.  for(i=0;i<nsteps;i++)
  230.   for(j=0;j<totmet;j++)
  231.   {
  232.    if(j==0)
  233.    {
  234.     Buff = strchr( Buff, '\n' );
  235.     if( Buff != NULL )
  236.     {
  237.      Buff++;
  238.      for( ;*Buff == ' '; Buff++);
  239.     }
  240.    }
  241.    else
  242.    {
  243.     Buff = strchr( Buff, ' ' );
  244.     if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  245.    }
  246.    if ( (Buff == NULL) || (sscanf( Buff,"%u", &uaux ) < 1)
  247.      || (uaux < 0) )
  248.     return IDS_ERR_BAD_LOOP;
  249.    (*loop)[i][j] = uaux;
  250.   }
  251.  
  252.  /* read metabolite status and names from the buffer                            */
  253.  for(i=0;i<totmet;i++)
  254.  {
  255.   Buff = strchr( Buff, '\n' );
  256.   if ( (Buff == NULL) || (sscanf( ++Buff, "%d %21s", &daux, saux ) < 2) )
  257.    return IDS_ERR_BAD_INTMET;
  258.   intmet[i] = daux;
  259.   strcpy( metname[i], saux );
  260.  }
  261.  
  262.  /* point endofbuf to the end of the topology section                           */
  263.  if( endoftop != NULL )
  264.   *endoftop = Buff;
  265.  /* return and signal that function was successful                                      */
  266.  return 0;
  267. }
  268.  
  269. int BufToTree( char *Buff, char **endoftree )
  270. {
  271.  int i;
  272.  char  *paux;
  273.  int daux, daux1, daux2;
  274.  char caux[10];
  275.  float faux;
  276.  
  277.  /* copy the title from the buffer                                                              */
  278.  paux = strchr( Buff, '\n' );
  279.  if( paux == NULL ) return IDS_ERR_LOAD;
  280.  /* take care of CR if one existed before LF                                            */
  281.  if( *(paux-1)=='\r' ) *(paux-1) = '\0';
  282.  *paux = '\0';
  283.  if( strlen( Buff ) > 63 ) Buff[63] = '\0';
  284.  strcpy( tr.descr, Buff );
  285.  Buff = paux+1;
  286.  
  287.  /* read nnode from the buffer                                                                          */
  288.  if ( sscanf( Buff, "%d", &daux ) < 1 )
  289.   return IDS_ERR_LOAD;
  290.  tr.nnode = daux;
  291.  
  292.  Buff = strchr( Buff, ' ' );
  293.  /* read nid from the buffer